//
//  StatusButtonView.m
//  KidReading
//
//  Created by telen on 14/12/18.
//  Copyright (c) 2014年 刘赞黄Telen. All rights reserved.
//

#import "StatusButtonView.h"

@implementation StatusButtonView
@end

@implementation StatusButton_2_View

- (instancetype)initWithFrame:(CGRect)frame imageOn:(NSString*)onStr imageOff:(NSString*)offStr
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
        [self addSubview:button];
        imageOnStr = onStr;
        imageOffStr = offStr;
        [self setButtonStatus:NO];
        //
        [button addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(clickDown) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchCancel];
        [button addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpOutside];
        [button setExclusiveTouch:YES];
    }
    return self;
}

- (void)updateImageOn:(NSString *)onStr imageOff:(NSString *)offStr
{
    imageOnStr = onStr;
    imageOffStr = offStr;
    [self setButtonStatus:_onOff];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    button.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    if (imgvOn) imgvOn.frame = button.frame;
    if (imgvOff) imgvOff.frame = button.frame;
}

- (void)setImageViewFrame:(CGRect)frame
{
    button.imageView.frame = frame;
    if (imgvOn) imgvOn.frame = frame;
    if (imgvOff) imgvOff.frame = frame;
}

- (void)setButtonStatus:(BOOL)status
{
    _onOff = status;
    if (_onOff) {
        [button setBackgroundImage:[UIImage imageNamed:imageOnStr] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:imageOffStr] forState:UIControlStateHighlighted];
        if (imgvOn) {
            imgvOn.hidden = NO;
            [imgvOn startAnimating];
        }
        if (imgvOff) {
            [imgvOff stopAnimating];
            imgvOff.hidden = YES;
        }
    }else{
        [button setBackgroundImage:[UIImage imageNamed:imageOffStr] forState:UIControlStateNormal];
        [button setBackgroundImage:[UIImage imageNamed:imageOnStr] forState:UIControlStateHighlighted];
        if (imgvOff) {
            imgvOff.hidden = NO;
            [imgvOff startAnimating];
        }
        if (imgvOn) {
            [imgvOn stopAnimating];
            imgvOn.hidden = YES;
        }
    }
}

- (void)setAni_UImageArr:(NSArray *)imageArr ani_duration:(NSTimeInterval)dur forStatus:(BOOL)status
{
    if (status && button) {
        if(imgvOn)[imgvOn removeFromSuperview];
        imgvOn = [[UIImageView alloc] initWithFrame:button.bounds];
        imgvOn.animationImages = imageArr;
        imgvOn.animationDuration = dur;
        imgvOn.animationRepeatCount = 0;//forever
        [button addSubview:imgvOn];
        [button sendSubviewToBack:imgvOn];//下沉图层
        imgvOn.hidden = YES;
    }
    if (!status && button) {
        if(imgvOff)[imgvOff removeFromSuperview];
        imgvOff = [[UIImageView alloc] initWithFrame:button.bounds];
        imgvOff.animationImages = imageArr;
        imgvOff.animationDuration = dur;
        imgvOff.animationRepeatCount = 0;//forever
        [button addSubview:imgvOff];
        [button sendSubviewToBack:imgvOff];//下沉图层
        imgvOff.hidden = YES;
    }
    [self setButtonStatus:_onOff];
}

- (void)setDisAbleImageStr:(NSString *)disStr
{
    [button setBackgroundImage:[UIImage imageNamed:disStr] forState:UIControlStateDisabled];
}

- (void)setDisAble:(BOOL)yn
{
    if (yn) {
        imgvOn.hidden = YES;
        imgvOff.hidden = YES;
        //
        button.enabled = NO;
    }else{
        button.enabled = YES;
        [self setButtonStatus:_onOff];
    }
}

- (void)clicked
{
    [self setButtonStatus:!_onOff];
    //
    if (self.delegate && [self.delegate respondsToSelector:@selector(statusButton_2_View_didClicked:)]) {
        [self.delegate statusButton_2_View_didClicked:self];
    }
    if (_btnClicked)_btnClicked(self);
    //
    UIView* view = button;
    [UIView animateWithDuration:0.1 animations:^{
        view.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

- (void)clickDown
{
    UIView* view = button;
    [UIView animateWithDuration:0.1 animations:^{
        view.transform = CGAffineTransformMakeScale(1.05, 1.05);
    }];
}

- (void)clickCancel
{
    UIView* view = button;
    [UIView animateWithDuration:0.1 animations:^{
        view.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

@end

#pragma mark-
@implementation StatusButton_2_ViewDefine
- (void)setButtonStatus:(BOOL)status
{
    [super setButtonStatus:status];
    if (_viewOn && _viewOn.superview == nil) {
        _viewOn.userInteractionEnabled = NO;
        [button addSubview:_viewOn];
        [button sendSubviewToBack:_viewOn];//下沉图层
    }
    if (_viewOff && _viewOff.superview == nil) {
        _viewOff.userInteractionEnabled = NO;
        [button addSubview:_viewOff];
        [button sendSubviewToBack:_viewOff];//下沉图层
    }
    if (_viewBg && _viewBg.superview == nil) {
        _viewBg.userInteractionEnabled = NO;
        [button addSubview:_viewBg];
        [button sendSubviewToBack:_viewBg];//下沉图层
    }
    _viewOn.hidden = status;
    _viewOff.hidden = !status;
}
@end

#pragma mark- 

@implementation StatusButton_3_View

- (instancetype)initWithFrame:(CGRect)frame imageNormal:(NSString *)nStr imageWork:(NSString *)wStr imageForbit:(NSString *)fStr
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = self.bounds;
        [self addSubview:button];
        imageNStr = nStr;
        imageWStr = wStr;
        imageFStr = fStr;
        [self setButtonStatus:0];
        //
        [button addTarget:self action:@selector(clicked) forControlEvents:UIControlEventTouchUpInside];
        [button addTarget:self action:@selector(clickDown) forControlEvents:UIControlEventTouchDown];
        [button addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchCancel];
        [button addTarget:self action:@selector(clickCancel) forControlEvents:UIControlEventTouchUpOutside];
        [button setExclusiveTouch:YES];
        //
        UISwipeGestureRecognizer* swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
        [swipe setDirection:UISwipeGestureRecognizerDirectionLeft];
        [self addGestureRecognizer:swipe];
        swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipe:)];
        [swipe setDirection:UISwipeGestureRecognizerDirectionRight];
        [self addGestureRecognizer:swipe];
    }
    return self;
}

- (void)swipe:(UISwipeGestureRecognizer*)sender
{
    if (sender.view == self && _delegate) {
        if(sender.direction == UISwipeGestureRecognizerDirectionLeft
           && [_delegate respondsToSelector:@selector(statusButton_3_View_swipeToLeft:)]){
            [_delegate statusButton_3_View_swipeToLeft:self];
        }
        else if(sender.direction == UISwipeGestureRecognizerDirectionRight
                && [_delegate respondsToSelector:@selector(statusButton_3_View_swipeToRight:)]){
            [_delegate statusButton_3_View_swipeToRight:self];
        }
    }
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    button.frame = self.bounds;
    if (imgvN) imgvN.frame = button.frame;
    if (imgvW) imgvW.frame = button.frame;
    if (imgvW) imgvF.frame = button.frame;
}

- (void)setButtonStatus:(StatusButton_3_View_Status)status
{
    _status = status;
    switch (status) {
        case StatusButton_3_View_Status_normal:
        {
            [button setBackgroundImage:[UIImage imageNamed:imageNStr] forState:UIControlStateNormal];
            if (imgvN) {
                imgvN.hidden = NO;
                [imgvN startAnimating];
            }
            if (imgvW) {
                [imgvW stopAnimating];
                imgvW.hidden = YES;
            }
            if (imgvF) {
                [imgvF stopAnimating];
                imgvF.hidden = YES;
            }
        }
            break;
        case StatusButton_3_View_Status_work:
        {
            [button setBackgroundImage:[UIImage imageNamed:imageWStr] forState:UIControlStateNormal];
            if (imgvW) {
                imgvW.hidden = NO;
                [imgvW startAnimating];
            }
            if (imgvN) {
                [imgvN stopAnimating];
                imgvN.hidden = YES;
            }
            if (imgvF) {
                [imgvF stopAnimating];
                imgvF.hidden = YES;
            }
        }
            break;
        case StatusButton_3_View_Status_forbit:
        {
            [button setBackgroundImage:[UIImage imageNamed:imageFStr] forState:UIControlStateNormal];
            if (imgvF) {
                imgvF.hidden = NO;
                [imgvF startAnimating];
            }
            if (imgvW) {
                [imgvW stopAnimating];
                imgvW.hidden = YES;
            }
            if (imgvN) {
                [imgvN stopAnimating];
                imgvN.hidden = YES;
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)setAni_UImageArr:(NSArray *)imageArr ani_duration:(NSTimeInterval)dur forStatus:(StatusButton_3_View_Status)status
{
    if (button) {
        switch (status) {
            case StatusButton_3_View_Status_normal:
            {
                if(imgvN)[imgvN removeFromSuperview];
                imgvN = [[UIImageView alloc] initWithFrame:button.bounds];
                imgvN.animationImages = imageArr;
                imgvN.animationDuration = dur;
                imgvN.animationRepeatCount = 0;//forever
                [button addSubview:imgvN];
                [button sendSubviewToBack:imgvN];//下沉图层
                imgvN.hidden = YES;
            }
                break;
            case StatusButton_3_View_Status_work:
            {
                if(imgvW)[imgvW removeFromSuperview];
                imgvW = [[UIImageView alloc] initWithFrame:button.bounds];
                imgvW.animationImages = imageArr;
                imgvW.animationDuration = dur;
                imgvW.animationRepeatCount = 0;//forever
                [button addSubview:imgvW];
                [button sendSubviewToBack:imgvW];//下沉图层
                imgvW.hidden = YES;
            }
                break;
            case StatusButton_3_View_Status_forbit:
            {
                if(imgvF)[imgvF removeFromSuperview];
                imgvF = [[UIImageView alloc] initWithFrame:button.bounds];
                imgvF.animationImages = imageArr;
                imgvF.animationDuration = dur;
                imgvF.animationRepeatCount = 0;//forever
                [button addSubview:imgvF];
                [button sendSubviewToBack:imgvF];//下沉图层
                imgvF.hidden = YES;
            }
                break;
                
            default:
                break;
        }
        [self setButtonStatus:_status];
    }
}

- (void)clicked
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(statusButton_3_View_didClicked:)]) {
        [self.delegate statusButton_3_View_didClicked:self];
    }
    if (_btnClicked)_btnClicked(self);
}

- (void)clickDown
{
    UIView* view = button;
    [UIView animateWithDuration:0.1 animations:^{
        view.transform = CGAffineTransformMakeScale(1.05, 1.05);
    }];
}

- (void)clickCancel
{
    UIView* view = button;
    [UIView animateWithDuration:0.1 animations:^{
        view.transform = CGAffineTransformMakeScale(1, 1);
    }];
}

@end


